﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// UI界面动画管理器，控制UI窗口的出现，暂停，关闭动画
    /// </summary>
    public class UIAnimManager : MonoBehaviour
    {
        //进入显示动画
        public void StartEnterAnim(UIWindowBase ui,UICallBack callBack)
        {
            UISystemEvent.Dispatch(ui, UIEvent.OnStartEnterAnim);
            ui.Show();
            StartCoroutine(ui.EnterAnim(EndEnterAnim, callBack));
        }


        public void EndEnterAnim(UIWindowBase ui, UICallBack callBack)
        {
            UISystemEvent.Dispatch(ui, UIEvent.OnCompleteEnterAnim);
            ui.OnCompleteEnterAnim();
            ui.windowStatus = UIWindowBase.WindowStatus.Open;//openAnim -> open

            try
            {
                if (callBack != null)
                {
                    callBack(ui);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }

        }

        //开始暂停动画
        public void StartPauseAnim(UIWindowBase UIbase, UICallBack callBack)
        {
            UISystemEvent.Dispatch(UIbase, UIEvent.OnStartPauseAnim);
            StartCoroutine(UIbase.PauserAnim(EndPauserAnim, callBack));
        }

        //暂停动画播放完毕回调
        public void EndPauserAnim(UIWindowBase UIbase, UICallBack callBack)
        {
            UISystemEvent.Dispatch(UIbase, UIEvent.OnCompletePauseAnim);
            UIbase.OnCompletePauserAnim();
            UIbase.windowStatus = UIWindowBase.WindowStatus.Pause;

            try
            {
                if (callBack != null)
                {
                    callBack(UIbase);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }



        //开始调用退出动画
        public void StartExitAnim(UIWindowBase UIbase, UICallBack callBack)
        {
            UISystemEvent.Dispatch(UIbase, UIEvent.OnStartExitAnim);
            StartCoroutine(UIbase.ExitAnim(EndExitAnim, callBack));
        }

        //退出动画播放完毕回调
        public void EndExitAnim(UIWindowBase UIbase, UICallBack callBack)
        {
            UISystemEvent.Dispatch(UIbase, UIEvent.OnCompleteExitAnim);
            UIbase.OnCompleteExitAnim();
            UIbase.windowStatus = UIWindowBase.WindowStatus.Close;
            try
            {
                if (callBack != null)
                {
                    callBack(UIbase);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.ToString());
            }
        }

    }
}
